package com.liuqi.openai.chain.impl;

import com.liuqi.openai.chain.AbstractOpenAiChain;
import com.liuqi.openai.chain.ChainExecutor;
import com.liuqi.openai.chain.result.ChainResult;
import com.liuqi.openai.data.message.UserMessage;
import com.liuqi.openai.memory.ChatMemory;
import com.liuqi.openai.model.OpenAiChatModel;
import com.liuqi.openai.tool.CustomTool;
import java.util.List;
import lombok.Builder;

import static com.liuqi.openai.util.ValidationUtil.ensureNotBlank;

/**
 * 简单的对话调用链
 *
 * @author liuqi
 * @date 2025/7/20
 **/
public class SimpleOpenAiChain extends AbstractOpenAiChain<String> {

    @Builder
    public SimpleOpenAiChain(String id,
                             OpenAiChatModel chatModel,
                             ChatMemory chatMemory,
                             List<? extends CustomTool> customTools) {
        super(id, chatModel, chatMemory, customTools);
    }

    @Override
    protected ChainResult doExecute(String message, ChainExecutor executor) throws Throwable {
        // 在发送消息队列中添加当前时间，主动告知AI的当前时间
        executor.addCurrentDatetime();

        // 构建用户消息
        UserMessage userMessage = UserMessage.from(ensureNotBlank(message, "userMessage"));

        // 执行大模型请求
        ChainResult result = executor.execute(userMessage);

        return result;
    }

}
